// api of the web assembly project
var Apis = {};
Apis.canvas = document.getElementById("canvas");
Apis.ctx = Apis.canvas.getContext("2d");
Apis.exception = function (message) {
    let stderr = document.getElementById("stderr");
    stderr.innerText += message + "\n";
    stderr.style.display = "block";
}
Apis.api = {
    line: function (x1, y1, x2, y2, color, width) {
        ctx.beginPath()
        ctx.moveTo(x1, y1)
        ctx.lineTo(x2, y2)
        ctx.lineWidth = width
        ctx.strokeStyle = color
        ctx.stroke()
        ctx.closePath()

    },

    rect: function (x, y, width, height, fill, stroke, width) {

        ctx.beginPath()
        ctx.rect(x, y, w, h)
        ctx.fillStyle = fill
        ctx.fill()
        if(stroke==null)
        {
          ctx.lineWidth = 0
          ctx.strokeStyle = null
          ctx.stroke()
        }
        else if(stroke!=null&&stroke_width==null)
        {
            ctx.lineWidth = 2
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        else if(stroke!=null&&stroke_width!=null)
        {
            ctx.lineWidth = stroke_width
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        ctx.closePath()
    },
    circle: function (x, y, r, a0, a1, fill, stroke, stroke_width, stroke_all) {
        ctx.beginPath() 
        ctx.arc(x, y, r, a0 * Math.PI / 180, a1 * Math.PI / 180)
        ctx.fillStyle = fill
        ctx.fill()
        if(stroke==null)
        {
        ctx.lineWidth = 0
        ctx.strokeStyle = null
        ctx.stroke()
        }
        else if(stroke!=null&&stroke_width==null)
        {
            ctx.lineWidth = 2
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        else if(stroke!=null&&stroke_width!=null)
        {
            ctx.lineWidth = stroke_width
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        if(stroke_all)
        {
            ctx.closePath()
        }
    

    },

    text: function (text, x, y, font, fill, stroke, stroke_width) {
        ctx.beginPath()
        ctx.font = fontSize + "px Arial"
        ctx.fillStyle = fill
        ctx.fill()
        if(stroke==null)
        {
            ctx.lineWidth = 0
            ctx.strokeStyle = null
            ctx.stroke()
        }
        else if(stroke!=null&&stroke_width==null)
        {
            ctx.lineWidth = 2
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        else if(stroke!=null&&stroke_width!=null)
        {
            ctx.lineWidth = stroke_width
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        ctx.fillText(text, x, y)
        ctx.strokeText(text, x, y)
        ctx.closePath()

    },
    triangle: function (x1, y1, x2, y2, x3, y3, fill, stroke, stroke_width) {

        ctx.beginPath()
        ctx.moveTo(x1, y1)
        ctx.lineTo(x2, y2)
        ctx.lineTo(x3, y3)
        ctx.closePath()
        if(stroke==null)
        {
            ctx.lineWidth = 0
            ctx.strokeStyle = null
            ctx.stroke()
        }
        else if(stroke!=null&&stroke_width==null)
        {
            ctx.lineWidth = 2
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        else if(stroke!=null&&stroke_width!=null)
        {
            ctx.lineWidth = stroke_width
            ctx.strokeStyle = stroke
            ctx.stroke() 
        }
        ctx.fillStyle = fill 
        ctx.fill()
        ctx.closePath()
    },
    speek: function (text) {
        if ('speechSynthesis' in window) {
            const utterance = new SpeechSynthesisUtterance(text);
            // 设置语言为简体中文
            utterance.lang = 'zh-CN';

            // 可选配置
            utterance.pitch = 1; // 设置音高（1 是默认值）
            utterance.rate = 1; // 设置语速（1 是默认值）
            utterance.volume = 1; // 设置音量（1 是最大值）
            utterance.onend = function () {

            };
            window.speechSynthesis.speak(utterance);
        } else {
            Apis.exception('[浏览器不支持语音合成]\n' + str);
        }
    },
    image: function (url, x, y, w, h) {
    },
    sound: function (url) {
    },

};

